﻿//    Copyright 2010,2011 Himanshu Vasishth
//   
//    This file is part of SecretVault.
//
//    SecretVault is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    SecretVault is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with SecretVault.  If not, see <http://www.gnu.org/licenses/>.
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;
using System.IO;

namespace PasswordLocker.UI
{
    public partial class OpenStoreForm : Form
    {
        public event EventHandler OpenStore;
        public event EventHandler CreateNew;

        public OpenStoreForm()
        {
            InitializeComponent();

            Text = string.Format(Resources.OpenVaultTitle, Resources.AppName);
        }

        public string StoreLocation
        {
            set { storeLocationTextBox.Text = value; }
            get { return storeLocationTextBox.Text; }
        }

        public string MasterPassword
        {
            get { return passwordTextBox.Text; }
        }

        private void OpenStoreClick(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            if(!ValidateChildren())
            {
                return;
            }

            if(OpenStore != null)
            {
                OpenStore(this, e);
            }
        }

        private void CreateNewVaultClick(object sender, EventArgs e)
        {
            if(CreateNew != null)
            {
                CreateNew(this, e);
            }
        }

        private void BrowseStoreClicked(object sender, EventArgs e)
        {
            var selectFileLocationDialog = new OpenFileDialog();
            try
            {
                selectFileLocationDialog.InitialDirectory = Path.GetDirectoryName(StoreLocation);
                selectFileLocationDialog.FileName = StoreLocation;
            }
            catch (ArgumentException)
            { 
                // Eat 
            }
            catch( PathTooLongException)
            {
                // Eat
            }
            selectFileLocationDialog.CheckFileExists = false;
            selectFileLocationDialog.DefaultExt = Constants.FileExtension;
            selectFileLocationDialog.Filter =
                string.Format("Password Locker Files(*.{0})|*.{0}", Constants.FileExtension);

            var dialogResult = selectFileLocationDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                StoreLocation = selectFileLocationDialog.FileName;
            }
        }

        private void passwordTextBox_Validating(object sender, CancelEventArgs e)
        {
            if(string.IsNullOrEmpty(passwordTextBox.Text))
            {
                e.Cancel = true;
                errorProvider1.SetError(passwordTextBox, 
                    string.Format(Resources.EmptyStringError, Resources.PasswordText));
            }
        }

        private void storeLocationTextBox_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(storeLocationTextBox.Text.Trim()))
            {
                e.Cancel = true;
                errorProvider1.SetError(storeLocationTextBox,
                    Resources.MisstingStoreError);
            }
        }
    }
}
